package com.yequan.thread.semaphore;

import java.util.concurrent.Semaphore;

/**
 * @author yq
 * @date 2020/09/04
 */
public class SemaphoreDemo2 {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(1,true);
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    Thread.sleep(1000);
                    System.out.println(finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            }).start();
        }
    }

}
